<template>
  <div>
    <!-- 搜索 -->
    <el-form :inline="true" size="mini">
      <el-form-item label="题库分类">
        <el-cascader
          v-model="form.classify"
          :options="cateList"
          :props="{
            value: 'id',   // 用 ID 字段做为值
            label: 'name',  // 用 name 字段做为标题
            children: 'child',  // 用 child 代表子元素
            emitPath: false   // 只返回子节点的ID
          }"
        >
        </el-cascader>
      </el-form-item>
      <el-form-item label="题库名称">
        <el-input v-model="form.name" placeholder="请输入题库名称关键字"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="search">搜索</el-button>
        <el-button>重置</el-button>
      </el-form-item>
    </el-form>
    <div>
      <el-button type="primary">添加新题库</el-button>
      <el-checkbox
      :true-label="1"
      :false-label="''"
      v-model="form.uid">只看我创建的</el-checkbox>
    </div>

    <!-- 表格 -->
    <el-table border :data="list">
      <el-table-column label="题库名称" prop="name">
      </el-table-column>
      <el-table-column label="所属分类" prop="classify">
      </el-table-column>
      <el-table-column label="题目数" prop="ques_amount">
      </el-table-column>
      <el-table-column label="创建人" prop="creator">
      </el-table-column>
      <el-table-column label="更新时间">
        <template slot-scope="scope">
          {{scope.row.updated_at | formatDate}}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template>
          <el-link>管理试题</el-link>
          <el-link>编辑</el-link>
          <el-link>删除</el-link>
        </template>
      </el-table-column>
    </el-table>

    <!-- 翻页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="form.page"
      :page-size="form.limit"
      :page-sizes="[3, 6, 9, 12]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 添加表单 -->
    <el-dialog title="添加新题库" :visible.sync="showDialog">
      <el-form label-width="100px">
        <el-form-item label="名称">
          <el-input v-model="addForm.name" placeholder="请输入题库名称关键字"></el-input>
        </el-form-item>
        <el-form-item label="分类">
          <el-cascader
            v-model="addForm.classify_id"
            :options="cateList"
            :props="{
              value: 'id',   // 用 ID 字段做为值
              label: 'name',  // 用 name 字段做为标题
              children: 'child',  // 用 child 代表子元素
              emitPath: false   // 只返回子节点的ID
            }"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="范围">
          <el-checkbox
          :true-label="2"
          :false-label="1"
          v-model="addForm.scope">仅创建人和超级管理员可查看管理使用</el-checkbox>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import axios from 'axios'
export default {
  data() {
    return {
      total: 0, // 总的记录数
      list: [], // 数据列表
      form: {
        page: 1,
        limit: 10,
        name: '',
        classify: '',
        uid: ''
      },
      cateList: [], // 分类列表
      addForm: {
        classify_id: '',
        name: '',
        scope: 1 // 不勾选 1 ，勾选 2
      },
      showDialog: true
    }
  },
  watch: {
    'form.uid': function() {
      this.getDataList()
    }
  },
  // 组件创建之前就获取数据
  created() {
    this.getDataList()

    // 获取题库分类的数据
    axios.get('/ques/classify').then(res => {
      if (res) {
        this.cateList = res.data
      }
    })
  },
  methods: {
    // 当每页条数改变时
    handleSizeChange(size) {
      this.form.limit = size

      this.getDataList()
    },
    // 当前页改变时
    handleCurrentChange(page) {
      this.form.page = page

      this.getDataList()
    },
    // 调用接口
    getDataList() {
      axios.get('/ques/bank', {
        params: this.form
      }).then(res => {
        if (res) {
          this.total = res.data.total
          this.list = res.data.list
        }
      })
    },
    // 搜索
    search() {
      this.form.page = 1
      this.getDataList()
    }
  }
}
</script>
